<?php

session_start();

require_once("includes/connect.php");
require_once("includes/function.php");

$task = $_REQUEST["task"];
$pet_id = $_REQUEST["pet_id"];
$id = $_REQUEST["id"];

$reason = $_REQUEST["reason"];

$i_date = $_REQUEST["i_date"];

$cheque_no = $_REQUEST["cheque_no"];
$branch_name = $_REQUEST["branch_name"];
$bank_name = $_REQUEST["bank_name"];

$val = $_REQUEST["val"];

$val1 = explode(",", $val);
$val1 = implode("','", $val1);

$cancel_date = $_REQUEST["cancel_date"];
$cancel_reason = $_REQUEST["cancel_reason"];

$db = new myDBS;
$db->selectDB("account");

switch ($task) {
    case "verify":

        $budget_year = (date("Y") < 2556) ? date("Y") + 543 : date("Y");

        $finance_id = get_running_finance($budget_year);
        
        $sql = "UPDATE tbl_welfare "
                . " SET verify_status = 'ผ่าน' "
                . " , verify_date = NOW() "
                . ", finance_id = '$finance_id' "
                . " WHERE pet_id = '$pet_id'";

        break;
    case "cancel":

        $sql = "UPDATE tbl_welfare "
                . " SET status = 'ยกเลิก' "
                . ", cancel_reason = '$cancel_reason' "
                . ", cancel_date = '$cancel_date' "
                . "  WHERE  pet_id = '$id'";

//        $sql = "UPDATE tbl_welfare "
//            . "SET verify_status = 'ยกเลิก'"
//            . ", reason = '$reason'"
//            . ", verify_date = NOW() "
//            . "WHERE  pet_id = '$pet_id'";

        break;
    case "approved":
        $sql = "UPDATE tbl_welfare "
                . "SET approved_status = 'อนุมัติ'"
                . ", approved_date = '$i_date'  "
                . "WHERE pet_id = '$pet_id'";

        break;
    case "disapproval":
        $sql = "UPDATE tbl_welfare "
                . "SET approved_status = 'ไม่อนุมัติ'"
                . ", approved_date = NOW() "
                . "WHERE pet_id = '$pet_id'";

        break;
    case "approvedCheque":
        $sql = "UPDATE tbl_welfare "
                . "SET cheque_status = 'อนุมัติ'"
                . ", cheque_date = '$i_date' "
                . "WHERE cheque_no = '$cheque_no'";

        break;
    case "disapprovalCheque":

        $sql = "UPDATE tbl_welfare "
                . "SET cheque_status = 'ไม่อนุมัติ' "
                . "WHERE cheque_no = '$cheque_no'";
        break;
    case "print":
        $sql = "UPDATE tbl_welfare "
                . "SET cheque_no = '$cheque_no'"
                . ", branch_name = '$branch_name'"
                . ", bank_name = '$bank_name'"
                . ", cheque_date = '$i_date' "
                . "WHERE pet_id in('$val1')";

        break;
    case "paid":
        $sql = "UPDATE tbl_welfare "
                . "SET paid_status = 'จ่ายแล้ว'"
                . ", paid_date = NOW() "
                . "WHERE cheque_no = '$cheque_no'";
        break;
}
//echo $sql;
//$db->runSQL($sql);
$db->runSQL($sql) or die(mysql_error());
?>
